import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'


// 创建路由实例
const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),  // history模式
  history: createWebHashHistory(import.meta.env.BASE_URL), // hash模式
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/pinia',
      name: 'pinia',
      component: () => import("@/views/PiniaView.vue")
    },
    {
      path: '/pinia/better',
      name: 'piniaBetter',
      component: () => import("@/views/PiniaBetterView.vue")
    },
    {
      path: '/404',
      name: 'notfound',
      component: () => import("@/views/NotFoundView.vue")
    },
    {
      // Vue2  匹配所有路由
      // path:"*",

      // Vue3  匹配所有路由
      path:"/:pathMatch(.*)*",
      redirect:(to)=>{
        return {name:"notfound",query:{wd:to.fullPath}}
      }
    }

  ]
})


// router.beforeEach((to,from,next)=>{
//   console.log("to",to);
//   console.log("from",from);
//   console.log("next",next);

//   next();
// })

// router.beforeEach((to,from)=>{
//   // console.log("to",to);
//   // console.log("from",from);

//   // return true; // next()   允许跳转
//   // return false;  // next(false)   阻止导航跳转

//   if(to.path=="/about"){
//     // return "/404";  // next("/404")
//     return {name:"notfound"};  // next({name:"notfound"})
//   }else{
//     return true;
//   }
// })


export default router
